package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.InvManufacturer;
import com.ysd.lis.mapper.inv.InvManufacturerMapper;
import com.ysd.lis.request.inv.InvManufacturerDto;
import com.ysd.lis.request.qc.ManufacturerPageParam;
import com.ysd.lis.service.inv.InvManufacturerService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-09-11
 */
@Service
public class InvManufacturerServiceImpl extends ServiceImpl<InvManufacturerMapper, InvManufacturer> implements InvManufacturerService {
    @Autowired
    InvManufacturerMapper invManufacturerMapper;
    @Override
    public Result findInvManufacturerPageList(ManufacturerPageParam pageParam) {
        Page<InvManufacturer> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<InvManufacturer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvManufacturer::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvManufacturer::getManuName, pageParam.getSeachValue())
                    .or().like(InvManufacturer::getManuCode, pageParam.getSeachValue())
                    .or().like(InvManufacturer::getInputCode,pageParam.getSeachValue()));
        }

        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(InvManufacturer::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(InvManufacturer::getSeq);
        Page<InvManufacturer> manufacturerPage = invManufacturerMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", manufacturerPage);
    }

    @Override
    public Result addInvManufacturer(InvManufacturer manufacturer) {
        int insert = invManufacturerMapper.insert(manufacturer);
        if(insert>0){
            return Result.succ(1,"添加成功",manufacturer);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editInvManufacturer(InvManufacturer manufacturer) {
        int updateById = invManufacturerMapper.updateById(manufacturer);
        if(updateById>0){
            return Result.succ(1,"提交成功",manufacturer);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteInvManufacturer(InvManufacturer manufacturer) {
        int deleteById = invManufacturerMapper.deleteById(manufacturer.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteInvManufacturerList(InvManufacturerDto dto) {
        List<InvManufacturer> manufacturerList = dto.getManufacturerList();
        List<String> ids = manufacturerList.stream().map(InvManufacturer::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result findInvManufacturerList(ManufacturerPageParam pageParam) {
        LambdaQueryWrapper<InvManufacturer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvManufacturer::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvManufacturer::getManuName, pageParam.getSeachValue())
                    .or().like(InvManufacturer::getManuCode, pageParam.getSeachValue())
                    .or().like(InvManufacturer::getInputCode,pageParam.getSeachValue()));
        }

        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(InvManufacturer::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(InvManufacturer::getSeq);
        List<InvManufacturer> manufacturerPage = invManufacturerMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", manufacturerPage);
    }
}
